﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Rej;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Rej
{
    public class rej_tblCust_MemberDAL:Irej_tblCust_Member
    {
        HKEA_CMSEntities model;
        rej_tblCust_Member member;

        public rej_tblCust_MemberDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static rej_tblCust_MemberDAL GetInstance()
        {
            return new rej_tblCust_MemberDAL();
        }

        public int InsertENT(rej_tblCust_Member ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.rej_tblCust_Member.AddObject(ent);
            return model.SaveChanges();
        }

        public List<rej_tblCust_Member> GetListByENT(rej_tblCust_Member ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.rej_tblCust_Member.Where(a => a.MemberUID > 0);
            if (ent.MemberUID > 0)
                result = result.Where(a => a.MemberUID == ent.MemberUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<rej_tblCust_Member> GetListByENT(rej_tblCust_Member ent)
        {
            var result = model.rej_tblCust_Member.Where(a => a.MemberUID > 0);
            if (ent.MemberUID > 0)
                result = result.Where(a => a.MemberUID == ent.MemberUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(rej_tblCust_Member ent)
        {
            var result = model.rej_tblCust_Member.Where(a => a.MemberUID > 0);
            if (ent.MemberUID > 0)
                result = result.Where(a => a.MemberUID == ent.MemberUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(rej_tblCust_Member ent)
        {
            member = model.rej_tblCust_Member.Where(a=>a.MemberUID==ent.MemberUID).FirstOrDefault();
            if (member != null)
            {
                member.Deleted = ent.Deleted;
                member.LastModifyBy = ent.LastModifyBy;
                member.LastModifyDate = DateTime.Now;
                member.CustomerUID = ent.CustomerUID;
                member.ExpiryDate = ent.ExpiryDate;
                member.JoinDate = ent.JoinDate;
                member.MemberID = ent.MemberID;
                member.MemStatusUID = ent.MemStatusUID;
                member.MemTypeUID = ent.MemTypeUID;
                member.RejectedBy = ent.RejectedBy;
                member.RejectedDate = ent.RejectedDate;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(rej_tblCust_Member ent)
        {
            var temp = model.rej_tblCust_Member.Where(a => a.MemberUID > 0);
            if (ent.MemberUID > 0)
                temp = temp.Where(a=>a.MemberUID==ent.MemberUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.rej_tblCust_Member.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
